﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using EPiServer.Web;
using EPiServer.Web.Hosting;
using System.Collections.Specialized;
using System.IO;
using System.Globalization;
using System.Configuration;

namespace EPiServer.CdnSupport.Test
{
    /// <summary>
    /// Summary description for TestRewrite
    /// </summary>
    [TestClass]
    public class TestRewrite
    {
        public Cdn cdn;

        [AssemblyInitialize()]
        public static void InitializeNewConfig(TestContext context)
        {
            ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
            configFileMap.ExeConfigFilename = "EPiServer.CdnSupport.Test.dll.config";
            System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
            EPiServer.Configuration.Settings.InitializeAllSettings(config);
            EPiServer.Configuration.Settings.Instance = EPiServer.Configuration.Settings.MapHostToSettings("*", true);
        }


        private UrlRewriteEventArgs GetValidUrlRewriteEventArgs()
        {
            return GetValidUrlRewriteEventArgs("/PageFiles/logo.png");
        }

        private UrlRewriteEventArgs GetValidUrlRewriteEventArgs(string url)
        {
            UrlRewriteEventArgs e = new UrlRewriteEventArgs(new UrlBuilder(url));
            e.IsModified = false;
            return e;
        }

        [TestInitialize]
        public void Init()
        {
            cdn = new Cdn("http://cdn.local", false,".png");

            GenericHostingEnvironment.Instance = new UnitTestHostingEnvironment();
            NameValueCollection coll = new NameValueCollection();
            coll.Add("virtualPath", "~/PageFiles/");
            TestVirtualPathProvider vpp = new TestVirtualPathProvider("testVpp", coll);
            GenericHostingEnvironment.RegisterVirtualPathProvider(vpp);
        }

        [TestMethod]
        public void Should_Rewrite_Valid_Url()
        {
            cdn.EnableTimestamp = false;
            UrlRewriteEventArgs e = GetValidUrlRewriteEventArgs();
            Assert.AreEqual("http://cdn.local/PageFiles/logo.png", cdn.RewriteUrl(e.Url, delegate(string path) { Assert.Fail("Should never get here"); return null; }).ToString());
        }

        [TestMethod]
        public void Should_Add_Timestamp()
        {
            cdn.EnableTimestamp = false;
            UrlRewriteEventArgs e = GetValidUrlRewriteEventArgs();
            Assert.AreEqual("http://cdn.local/PageFiles/logo.png", cdn.RewriteUrl(e.Url, delegate(string path) { Assert.Fail("Should never get here"); return null; }).ToString());

            cdn.EnableTimestamp = true;
            e = GetValidUrlRewriteEventArgs();
            Assert.AreEqual("http://cdn.local/cdn-1bf54c5a567d880/PageFiles/logo.png", cdn.RewriteUrl(e.Url, delegate(string path) { Assert.Fail("Should never get here"); return null; }).ToString());

            e = GetValidUrlRewriteEventArgs("/images/DoesNotExist.png");
            Assert.AreEqual("/images/DoesNotExist.png", cdn.RewriteUrl(e.Url, delegate(string path) { return "c:\\blabla.png"; }).ToString());

            e = GetValidUrlRewriteEventArgs("/Images/DoesExist.png");
            FileInfo testFile = new FileInfo(System.Environment.CurrentDirectory + "\\EPiServer.CdnSupport.dll");
            Assert.AreEqual("http://cdn.local/cdn-" + testFile.LastWriteTime.ToFileTime().ToString("x8", CultureInfo.InvariantCulture) + "/Images/DoesExist.png", cdn.RewriteUrl(e.Url, delegate(string path) { return testFile.FullName; }).ToString());
        }

        [TestMethod]
        public void Should_Handle_Secure_Files()
        {
            cdn.EnableTimestamp = true;
            UrlRewriteEventArgs e = GetValidUrlRewriteEventArgs("/Secured/logo.png");
            Assert.AreEqual("/Secured/logo.png", cdn.RewriteUrl(e.Url, delegate(string path) { Assert.Fail("Should never get here"); return null; }).ToString());

            e = GetValidUrlRewriteEventArgs("/Open/logo.png");
            Assert.AreEqual("http://cdn.local/cdn-1bf54c5a567d880/Open/logo.png", cdn.RewriteUrl(e.Url, delegate(string path) { Assert.Fail("Should never get here"); return null; }).ToString());
        }
    }
}
